<?php

require_once($PATHS->get_hidden().'inc/_sys/site_dbc_pnt.class.php');


$conf_loader = new Config_Loader_File($PATHS->get_hidden().'conf/site_specific.conf');
$lda_on = $conf_loader->get_conf_value('SS_LANG_DETECTOR_ADVANCED');

if ($lda_on) {
	require_once($PATHS->get_hidden().'inc/modules/language_detector_advanced/language_detector_advanced.class.php');
	$lda_var_name = $conf_loader->get_conf_value('SS_LANG_DETECTOR_ADVANCED_VAR_NAME');
	$ld_lang_switch_var_name = $conf_loader->get_conf_value('SS_LANG_DETECTOR_ADVANCED_SWITCH_VAR_NAME');

	global $_SITE_DBC_PNT;
	if (!($_SITE_DBC_PNT instanceof DB_Connection)) {
		$db_config_loader = new DB_Config_Loader_File($PATHS->get_hidden().'conf/db.conf');
		$db_config = new DB_Config();
		$db_config_loader->load_db_config($db_config);

		$_SITE_DBC_PNT = new Site_DBC_PNT($db_config);
	}

	$context = new Web_Context();


	$ld = new Language_Detector_Advanced($context, $_SITE_DBC_PNT);

	global $WSC;

	global $_VISITOR_COOKIE;

	$lang = $ld->detect_language($WSC->get_available_languages_arr(), $_VISITOR_COOKIE->get_id(), $lda_var_name, $ld_lang_switch_var_name);

	if ($lang !== false) {
		$WSC->set_current_language($lang);
	} else {
		$WSC->set_current_language($WSC->get_default_language());
	}

	output_add_pi_rewrite_var($lda_var_name, $WSC->get_current_language());
}


